package Day14;

/**
 * @author SunYuHang
 * @date 2023-01-18 19:02
 * @ClassName : 使括号有效的最少添加  //类名
 */

public class 使括号有效的最少添加 {
    public int minAddToMakeValid(String s){
        int res = 0;
        int leftCount = 0;
        int length = s.length();
        for (int i = 0; i < length ; i++) {
            char c = s.charAt(i);
            if (c == '('){
                leftCount++;
            }else {
                if (leftCount>0){
                    leftCount--;
                }else {
                    res++;
                }
            }
        }
        res += leftCount;
        return res;
    }
}
